import math
import pyglet
from pyglet.window import key

# Set Size of Window
WinWidth = 640
WinHeight = 480
window = pyglet.window.Window(WinWidth, WinHeight)

GFXbatch = pyglet.graphics.Batch()
background = pyglet.graphics.OrderedGroup(0)
foreground = pyglet.graphics.OrderedGroup(1)

class ship(object):
    def __init__(self):
        ball_image = pyglet.image.load('ship.png')
        self.sprite = pyglet.sprite.Sprite(ball_image, x=50, y=50)
        self.key_handler = key.KeyStateHandler()
        self.bullets = []
        self.reload = 0
    def update(self, ts):
        global GFXbatch
        self.reload +=1
        if self.key_handler[key.LEFT]:
            self.sprite.x -= 4
        if self.key_handler[key.RIGHT]:
            self.sprite.x += 4
        if self.key_handler[key.X] and len(self.bullets)<10 and self.reload>10:
            self.bullets.append(bullet(GFXbatch, (self.sprite.x+25, self.sprite.y+25)))
            self.reload = 0

    def updateb(self, ts):
        for b in self.bullets:
            b.sprite.y += 8
            if b.sprite.y>480:
                self.removeBullet(b)
    def removeBullet(self, b):
        global GFXbatch
        self.bullets.remove(b)
        b.sprite.delete()

class bullet(object):
    def __init__(self, batch, pos):
        pattern = pyglet.image.SolidColorImagePattern((125, 0, 0, 255))
        image = pyglet.image.create(8, 8, pattern)
        self.sprite = pyglet.sprite.Sprite(img=image,x = pos[0], y = pos[1], batch=batch)

class target(object):
    def __init__(self, batch, pos):
        pattern = pyglet.image.SolidColorImagePattern((0, 125, 0, 255))
        image = pyglet.image.create(64, 64, pattern)
        self.sprite = pyglet.sprite.Sprite(img=image,x = pos[0], y = pos[1], batch=batch)

# Create text label
label = pyglet.text.Label('Shoot!', 
                          font_name='Arial', 
                          font_size=int(WinHeight/10),
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center',
                          batch=GFXbatch, group=foreground)
s = ship()
t = target(GFXbatch, (250, 216))

window.push_handlers(s.key_handler)

# Move on the text along the path
def update(ts):
    global s
    s.update(ts)
def update2(ts):
    global s,t
    s.updateb(ts)
    for b in s.bullets:
        br = (b.sprite.x, b.sprite.y, b.sprite.x + b.sprite.width, b.sprite.y + b.sprite.height)
        tr = (t.sprite.x, t.sprite.y, t.sprite.x + t.sprite.width, t.sprite.y + t.sprite.height)
        
        if not(br[0]>tr[2] or tr[0]>br[2] or br[1]>tr[3] or tr[1]>br[3]):
            print "hit!"
            t.sprite.y += 10
            if t.sprite.y>480: t.sprite.y=100
            s.removeBullet(b)

#def collision(rect1, rect2):

pyglet.clock.schedule_interval(update,  1/100.0)
pyglet.clock.schedule_interval(update2,  1/200.0)

@window.event
def on_draw():
    global GFXbatch
    window.clear() 
    GFXbatch.draw()
    s.sprite.draw()

pyglet.app.run()

